<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Role;
use App\Http\Requests;
use App\Depart;
use Auth;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $users = User::whereCompanyId(Auth::user()->company_id)->paginate(10);
        return view('user.index',[
            'users'=>$users
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $user = new User();
        $roles = Role::whereCompanyId($user->company_id)->get()->mapWithKeys(function($item){
            return [$item['id']=>$item['display_name']];
        });

        return view('user.form',[
            'user'=>$user,
            'departs'=>Depart::getDeparts(Auth::user()->company_id)->toArray(),
            'roles'=>$roles
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Requests\UserRequest $request)
    {
        $data = $request->input();
        $data['company_id'] = Auth::user()->company_id;
        $user = User::create($data);
        return redirect()->route('user.create');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = User::find($id);
        return view('user.show',[
            'user'=>$user
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find($id);
        $roles = Role::whereCompanyId($user->company_id)->get()->mapWithKeys(function($item){
            return [$item['id']=>$item['display_name']];
        });
        return view('user.form',[
            'user'=>$user,
            'departs'=>Depart::getDeparts(Auth::user()->company_id)->toArray(),
            'roles'=>$roles
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Requests\UserRequest $request, $id)
    {
        $data = $request->input();
        if(isset($data['password'])){
            $data['password'] = \Hash::make($data['password']);
        }
        $user = User::find($id);
        $user->fill($data);
        $user->save();
        return redirect()->route('user.edit',['id'=>$id]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        User::destroy($id);
        return redirect()->route('user.index');
    }
}
